﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fastyou.WCF.Contracts;
using System.ServiceModel;
using Fastyou.Model;
using System.Threading;

namespace Fastyou.WCF.ServiceLib
{
    public class CallbackEventArg :EventArgs
    {
        public Work Job
        {
            get;
            set;
        }
    }
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class Duplex:IDuplex
    {
        List<Work> jobss = new List<Work>();
        public delegate void ServerEventHandler(object sender, CallbackEventArg e);
        public static event ServerEventHandler OnAddJob;
        public static event ServerEventHandler OnStatusChanged;

        IDuplexCallback callback;

        #region IDuplex Members

        //public string DoWork(string workName)
        //{
        //    IDuplexCallback callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
        //    Console.WriteLine("this work is {0} .", workName);
        //    callback.DoneWork(DateTime.Now);
        //    return string.Format("this time is {0},work name is {1}", DateTime.Now.ToString(), workName);
        //}

        #endregion
        #region IDuplex Members

        public void Start()
        {
            callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
            OnAddJob += new ServerEventHandler(Duplex_OnAddJob);
            
            OnStatusChanged += new ServerEventHandler(Duplex_OnStatusChanged);
            Console.WriteLine("start");
        }

        void Duplex_OnStatusChanged(object sender, CallbackEventArg e)
        {
            if (e != null && e.Job != null)
            {
                callback.ChangeStatus(e.Job);
            }
        }

        void Duplex_OnAddJob(object sender, CallbackEventArg e)
        {
            if (e != null && e.Job != null)
            {
                callback.AddJobCall(e.Job);
            }
        }

        Work GetJob(string jobName)
        {
            foreach (Work inwork in jobss)
            {
                if (inwork.WorkName == jobName)
                {
                    return inwork;
                }
            }
            return null;
        }
        public void DoWork(string jobName)
        {
            Work inwork = GetJob(jobName);
            if (inwork != null)
            {
                if (OnStatusChanged != null)
                {
                    CallbackEventArg e = new CallbackEventArg();
                    inwork.status = "runing";
                    e.Job = inwork;
                    Console.WriteLine("Job {0} is runing.", inwork.WorkName);
                    OnStatusChanged(new object(),e);
                    //Thread.Sleep(1000 * 10);
                    inwork.status = "ok";
                    Console.WriteLine("Job {0} is ok.", inwork.WorkName);
                    OnStatusChanged(new object(), e);
                }
            }
        }

        public List<Work> Jobs()
        {
            return jobss; 
        }

        public void AddJob(Fastyou.Model.Work job)
        {

			if (job != null)
			{
				if (OnAddJob != null)
				{
					CallbackEventArg e = new CallbackEventArg();
					job.status = "ok";
					e.Job = job;
					jobss.Add(job);
					Console.WriteLine("Job {0} has been added.", job.WorkName);
					OnAddJob(new object(), e);
				}
			}
        }

        #endregion
    }
}
